home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / ARCADE.ZIP / MS.EXE / lzh / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-15  |  6.4 KB  |  293 lines

  1. /*------------------------------misc.c--------------------------------------*/
  2. /*
  3. Copyright 1992 David Conger
  4. */
  5.  
  6.  
  7.  
  8. /*---------------------------include files----------------------------------*/
  9.  
  10. #include <bios.h>
  11. #include "misc.h"
  12. #include <time.h>
  13. #include <stdarg.h>
  14. #include <graphics.h>
  15. #include <dos.h>
  16. #include <stdio.h>
  17. #include "game.h"
  18.  
  19. /*-------------------------end include files--------------------------------*/
  20.  
  21.  
  22.  
  23.  
  24. /*---------------------------local constants--------------------------------*/
  25.  
  26. #define ASCII_CHAR_MASK     0x00FF
  27.  
  28. #define TICKS_PER_SECOND        1024
  29. #define SECONDS_PER_MINUTE        60
  30. #define MINUTES_PER_HOUR        60
  31.  
  32. #define INTERRUPT_70H                        0x70
  33. #define    INTERRUPT_15H                        0x15
  34. #define SET_REAL_TIME_CLOCK_INTERVAL        0x83
  35. #define TIMER_REMOVAL_VALIDATION_DELAY        200
  36. #define REAL_TIME_INTERRUPT_COUNT            0xFFFF
  37. #define REAL_TIME_INTERRUPT_POKE_SEGMENT    0x40
  38. #define REAL_TIME_INTERRUPT_POKE_OFFSET        0x9C
  39. #define ARBITRARY_VALUE                        0x500
  40.  
  41. /*-------------------------end local constants------------------------------*/
  42.  
  43.  
  44.  
  45.  
  46. /*----------------------------module globals--------------------------------*/
  47.  
  48. game_timer time_since_initialization={0};
  49. static unsigned timer_flags;
  50. static void interrupt (*old_interrupt_vector)();
  51.  
  52. /*--------------------------end module globals------------------------------*/
  53.  
  54.  
  55.  
  56. /*----------------------------local prototypes------------------------------*/
  57.  
  58. void interrupt timer_interrupt_handler(void);
  59.  
  60. /*--------------------------end local prototypes----------------------------*/
  61.  
  62.  
  63.  
  64.  
  65.  
  66. /*---------------------------get_keystroke----------------------------------*/
  67.  
  68. int get_keystroke(int pause,int *special_key)
  69. {
  70.     int key;
  71.  
  72.  
  73.     *special_key=0;
  74.     key=bioskey(1);
  75.  
  76.     if ((!key) && (pause==WAIT))
  77.     {
  78.         while (!bioskey(1))
  79.             /* wait */;
  80.         key=1;
  81.     }
  82.  
  83.     if (key)
  84.     {
  85.         key=bioskey(0);
  86.  
  87.         if ((key & ASCII_CHAR_MASK)==0)
  88.         {
  89.             *special_key=(key>>8);
  90.             key=0;
  91.         }
  92.         else
  93.         {
  94.             *special_key=0;
  95.             key&=0x00FF;
  96.         }
  97.     }
  98.  
  99.     return(key);
  100. }
  101.  
  102. /*-------------------------end get_keystroke--------------------------------*/
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /*--------------------------------wait--------------------------------------*/
  109.  
  110. void wait(int seconds)
  111. {
  112.     time_t start_time,current_time;
  113.  
  114.     time(&start_time);
  115.     do
  116.     {
  117.         time(¤t_time);
  118.  
  119.     } while (difftime(current_time,start_time)<seconds);
  120. }
  121.  
  122. /*------------------------------end wait------------------------------------*/
  123.  
  124.  
  125.  
  126.  
  127.  
  128. /*------------------------------write_message--------------------------------*/
  129.  
  130. void write_message(int row,int col,char *format_str,...)
  131. {
  132.     va_list arg_list;
  133.     char message[MAX_MESSAGE_LEN];
  134.  
  135.  
  136.     moveto(col,row);
  137.     va_start(arg_list,format_str);
  138.     vsprintf(message,format_str,arg_list);
  139.     va_end(arg_list);
  140.     outtextxy(col,row,message);
  141. }
  142.  
  143. /*----------------------------end write_message------------------------------*/
  144.  
  145.  
  146.  
  147.  
  148. /*---------------------------install_timer----------------------------------*/
  149.  
  150. boolean install_timer(void)
  151. {
  152.     struct SREGS sreg;
  153.     union REGS inregs,outregs;
  154.     unsigned far *flag_ptr;
  155.  
  156.  
  157.     old_interrupt_vector=getvect(INTERRUPT_70H);
  158.     setvect(INTERRUPT_70H,timer_interrupt_handler);
  159.  
  160.     flag_ptr=(unsigned far *)(&timer_flags);
  161.     sreg.es=FP_SEG(flag_ptr);
  162.     inregs.x.bx=FP_OFF(flag_ptr);
  163.     inregs.h.ah=SET_REAL_TIME_CLOCK_INTERVAL;
  164.     inregs.h.al=0;
  165.     inregs.x.cx=ARBITRARY_VALUE;
  166.     inregs.x.dx=0;
  167.     int86x(INTERRUPT_15H,&inregs,&outregs,&sreg);
  168.  
  169.     return(outregs.x.cflag);
  170. }
  171.  
  172. /*-------------------------end install_timer--------------------------------*/
  173.  
  174.  
  175.  
  176.  
  177. /*----------------------------remove_timer----------------------------------*/
  178.  
  179. boolean remove_timer(void)
  180. {
  181.     disable();
  182.     setvect(INTERRUPT_70H,old_interrupt_vector);
  183.     delay(TIMER_REMOVAL_VALIDATION_DELAY);
  184.     enable();
  185.  
  186.     return(!timer_flags);
  187. }
  188.  
  189. /*--------------------------end remove_timer--------------------------------*/
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /*-----------------------timer_interrupt_handler----------------------------*/
  196.  
  197. void interrupt timer_interrupt_handler(void)
  198. {
  199.     poke(REAL_TIME_INTERRUPT_POKE_SEGMENT,REAL_TIME_INTERRUPT_POKE_OFFSET,
  200.          REAL_TIME_INTERRUPT_COUNT);
  201.  
  202.     time_since_initialization.ticks++;
  203.     if (time_since_initialization.ticks>TICKS_PER_SECOND)
  204.     {
  205.         time_since_initialization.ticks-=TICKS_PER_SECOND;
  206.  
  207.         time_since_initialization.seconds++;
  208.         if (time_since_initialization.seconds>SECONDS_PER_MINUTE)
  209.         {
  210.             time_since_initialization.seconds-=SECONDS_PER_MINUTE;
  211.  
  212.             time_since_initialization.minutes++;
  213.             if (time_since_initialization.minutes>MINUTES_PER_HOUR)
  214.             {
  215.                 time_since_initialization.minutes-=MINUTES_PER_HOUR;
  216.                 time_since_initialization.hours++;
  217.             }
  218.         }
  219.     }
  220.     if (old_interrupt_vector!=NULL)
  221.         (*old_interrupt_vector)();
  222. }
  223.  
  224. /*---------------------end timer_interrupt_handler--------------------------*/
  225.  
  226.  
  227.  
  228.  
  229. /*-----------------------------set_timer------------------------------------*/
  230.  
  231. void set_timer(game_timer *current_time)
  232. {
  233.     current_time->ticks=time_since_initialization.ticks;
  234.     current_time->seconds=time_since_initialization.seconds;
  235.     current_time->minutes=time_since_initialization.minutes;
  236.     current_time->hours=time_since_initialization.hours;
  237. }
  238.  
  239. /*---------------------------end set_timer----------------------------------*/
  240.  
  241.  
  242.  
  243. /*-----------------------------elapsed_time---------------------------------*/
  244.  
  245. game_time elapsed_time(game_timer *start_time)
  246. {
  247.     game_timer temp,current_time;
  248.     game_time answer;
  249.  
  250.  
  251.     current_time=time_since_initialization;
  252.  
  253.     temp.ticks=current_time.ticks - start_time->ticks;
  254.     if (temp.ticks<0)
  255.     {
  256.         current_time.seconds--;
  257.         temp.ticks+=TICKS_PER_SECOND;
  258.     }
  259.  
  260.     temp.seconds=current_time.seconds - start_time->seconds;
  261.     if (temp.seconds<0)
  262.     {
  263.         current_time.minutes--;
  264.         temp.seconds+=SECONDS_PER_MINUTE;
  265.     }
  266.  
  267.     temp.minutes=current_time.minutes - start_time->minutes;
  268.     if (temp.minutes<0)
  269.     {
  270.         current_time.hours--;
  271.         temp.seconds+=MINUTES_PER_HOUR;
  272.     }
  273.  
  274.     temp.hours=current_time.hours - start_time->hours;
  275.  
  276.     answer=temp.hours * MINUTES_PER_HOUR * SECONDS_PER_MINUTE *
  277.            TICKS_PER_SECOND;
  278.  
  279.     answer+=temp.minutes * SECONDS_PER_MINUTE * TICKS_PER_SECOND;
  280.  
  281.     answer+=temp.seconds * TICKS_PER_SECOND;
  282.  
  283.     answer+=temp.ticks;
  284.  
  285.     return(answer);
  286. }
  287.  
  288. /*---------------------------end elapsed_time-------------------------------*/
  289.  
  290.  
  291. /*----------------------------end misc.c------------------------------------*/
  292.  
  293.